package com.leyou.item.controller;

import com.Jshop.common.pojo.PageResult;
import com.leyou.item.Service.GoodService;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class GoodController {

    @Autowired
    private GoodService goodService;


    /**
     * 查询所有SPU，有分页限制
     * */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows
    ){
        PageResult<SpuBo> r = this.goodService.querySpuByPage(key, saleable, page, rows);

        if (CollectionUtils.isEmpty(r.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 新增商品
     * */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){
        this.goodService.saveGoods(spuBo);
        //204 更新成功
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品
     * */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        this.goodService.updateGoods(spuBo);
        //204 更新成功
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 前端点击编辑按钮 回显SpuDeail信息
     */
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId){

        SpuDetail spuDetail = this.goodService.querySpuDetailBySpuId(spuId);
        if (spuDetail==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);
    }


    /**
     * 前端点击编辑按钮 回显sku信息
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkusBySpuId(@RequestParam("id") Long spuId){

        List<Sku> skus = this.goodService.querySkusBySpuId(spuId);
        if (CollectionUtils.isEmpty(skus)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        Spu spu = this.goodService.querySpuById(id);
        if(spu == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(spu);
    }

    @GetMapping("sku/{id}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("id") Long id) {
        Sku sku = this.goodService.querySkuById(id);
        if (sku == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(sku);
    }


    @PostMapping("skus")
    public ResponseEntity<List<Sku>> querySkusByList(@RequestBody List<Long> ids) {
        if (ids == null || ids.size() == 0){
            return ResponseEntity.ok(new ArrayList<>());
        }
        List<Sku> skus = this.goodService.querySkusByList(ids);
        if (skus == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    /**
     * @Description 统一修改图片baseUrl fastdfs -> oss
     **/
    @GetMapping("sku/pu")
    public ResponseEntity<Void> modifyImgUrl() {
        this.goodService.modifyAll();
        return ResponseEntity.ok().build();
    }

    /**
     * @Description //商品sku库存递减
     * @Param
     * @return
     * @Date  2021/11/9
     **/
    @PostMapping(value = "decr")
    public ResponseEntity<Void> decrementStock(@RequestBody Map<String, Integer> decrMap){
        this.goodService.decrementStock(decrMap);
        return ResponseEntity.ok().build();
    }



}
